package com.bawei.module_repair

import android.content.DialogInterface
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.base.utils.LoggerUtils
import com.bawei.lib_common.base.utils.ToastUtils
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_repair.adapter.RepairAdapter
import com.bawei.module_repair.databinding.ActivityRepairBinding
import com.bawei.module_repair.viewmodel.RepairViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.RepairActivity)
class RepairActivity : BaseActivity<RepairViewModel,ActivityRepairBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_repair
    override val mViewModel: RepairViewModel by viewModels()
    private val mRepairAdapter: RepairAdapter by lazy {
        RepairAdapter()
    }

    override fun initView() {
        initAdapter()
    }

    override fun initListener() {
        mViewModel.repairEvent.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    mRepairAdapter.setRepairData(mViewModel.repairList.reversed())
                }
            }
        }
        mBinding.linearAdd.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.RepairAddActivity).navigation()
        }
        mRepairAdapter.onItemLoginClick = {
            AlertDialog.Builder(this@RepairActivity)
                .setTitle("是否删除维修订单?")
                .setPositiveButton("确定", object : DialogInterface.OnClickListener {
                    override fun onClick(p0: DialogInterface?, p1: Int) {
                        mViewModel.deleteRepair(it)
                    }
                })
                .setNegativeButton("取消", null)
                .show()
        }
        mViewModel.repairDeleteEvent.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this@RepairActivity,"删除成功")
                    mViewModel.getsRepairList()
                }
            }
        }
    }

    override fun initData() {
        mViewModel.getsRepairList()
    }
    private fun initAdapter() {
        mRepairAdapter.setupLayoutManager(mBinding.repairRec)
        mBinding.repairRec.adapter = mRepairAdapter
    }

}